package com.qingluan.geo;

import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import android.util.Log;
import com.qingluan.arguments.ARGUMENTS;
import com.qingluan.dd.InetTool.HttpPost;
import com.qingluan.dd.InetTool.HttpPost.POSTListener;



public class GeoGetAsync {
	private String name ;
	private String tag = this.getClass().getName();
	private String token_idString;
	private String token_keyString;
	//on or off
	
	private GetPoninterAsyncListener listener;
	private Context context_from_activity;
	
	//static values
	public  static ArrayList<String> IDS;
	
	public GeoGetAsync(Context context,String name,String token_id, String token_key) {
		// TODO Auto-generated constructor stub
		Log.d(tag, "Map load ok...");
		this.context_from_activity = context;
		this.name = name;
		
		this.token_idString = token_id;
		this.token_keyString = token_key;
		
		IDS = new ArrayList<String>(); 
	}
	
	
	public void run() {
		// TODO Auto-generated method stub
		Log.d(tag, "Map module load ok...");
	
		Log.d(tag, "Map running ...");
		JSONObject json = new JSONObject();
		
		try {
			JSONArray idsArray  = new JSONArray(IDS);
			json.put("name", this.name);
			json.put("user_key", this.token_keyString);
			json.put("user_id", this.token_idString);
			json.put("ids", idsArray);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Log.d(tag, "Map data post ...\n"+json.toString());
		HttpPost oneConnection =   new HttpPost(ARGUMENTS.POINT_GET_IP) ;
		oneConnection.JSONPOSTTemplate(ARGUMENTS.POST_POINT_GET,json );
		oneConnection.setPOSTTemplateListener(new POSTListener() {
			
			@Override
			public void successful(String info) {
				// TODO Auto-generated method stub
				Log.d(tag,"info :"+info);
				try {
					JSONObject json  = new JSONObject(info);
					JSONArray items  = json.getJSONArray("items");
					
					if (listener != null){
						listener.onGetPoint(items);
					}else {
						Log.d(tag, "Error GetPoninterAsyncListener is Null");
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
			
			@Override
			public void error(String error) {
				// TODO Auto-generated method stub
				Log.d(tag, "eroor : "+error);
				listener.errorLog(error);
			}
		});
		

	}
	
	
	public  void setGetPointListener(GetPoninterAsyncListener listener){
		this.listener = listener;
	}
	
	public void updateToken(String token_id,String token_key) {
		this.token_idString = token_id;
		this.token_keyString = token_key;
	}
	
	
	
	public interface GetPoninterAsyncListener{
		public void onGetPoint(JSONArray info);
		public void errorLog(String error);
	}
}
